/**
 * 右侧弹出层指令
 */
module.exports = function(workBarNoWorkService, $document,$cookies, $rootScope,$timeout,caseBarDetailjinchengService,pdfDelegate,caseBarDetailZhengService,workBarService,$state){
  return{
    restrict: "AE",
    templateUrl: "partials/workBar/detailPanel.html",
    transclude: true,
    // priority: 1002,
    scope: {
      detail: '=', //任务详情// action: "&"可以继承父级fn
      parentList:'=',//拿到父级列表
      current:'@' // index 值
    },
    link: function (scope, element, attrs) {
      if (!scope.detail.task) {
        return;
      }

      element.css({height:$(window).height()+'px'});
     scope.init=function(){
       scope.open = false;
       scope.tempDetail = angular.copy(scope.detail);
       //案件状态
       scope.taskType=scope.tempDetail.taskType;
       //归档类型
       scope.typeIf=$rootScope.typeIf;
       //详情taskId
       if(scope.tempDetail.task.id){
         scope.detailTaskId=scope.tempDetail.task.id;
       }
       //登录ID
       scope.loginId = kbt.decrypt($cookies.get('cookieUserId'));
       //用户名
       scope.userName=$cookies.get('userPortraitName');
       //案件ID
       if(scope.taskType==1){
         scope.caseId=scope.tempDetail.task.caseid
       }else{
         scope.caseId=scope.tempDetail.task.caseid;
       }
     }






      //详情编辑开关
      // scope.detailEditToggle;
      //编辑任务案件
      scope.EditTaskCase=EditTaskCase;
      //编辑任务状态
      scope.DetailEditToggle=DetailEditToggle;
      //关闭编辑任务状态
      scope.EditTaskStateClose=EditTaskStateClose;
      //编辑任务描述
      scope.EditCaseDescribe=EditCaseDescribe;
      scope.EditCaseDescribeTwo=EditCaseDescribeTwo;
      //关闭任务进程
      scope.EditTaskParticularsClose=EditTaskParticularsClose;
      //删除任务进程
      scope.jcdeleteupdateNameConfirm=jcdeleteupdateNameConfirm;
      //点击checkbox删除进程
      scope.detailThrothTheP=detailThrothTheP;
      //工时
      scope.EditTaskEstimateHour=EditTaskEstimateHour;
      scope.EditTaskActualHour=EditTaskActualHour;
      //提交数据
      scope.updateDes=updateDes;
      //检查项
      scope.EditCheckItem=EditCheckItem;
      scope.EditCheckItemClose=EditCheckItemClose
      scope.EditTaskCheckName=EditTaskCheckName;
      //检查项添加
      scope.saveTaskCheckedItem=saveTaskCheckedItem;
      //检查项 接口更新
      scope.updateTaskCheckedItem=updateTaskCheckedItem;
      scope.updateTaskCheckedItemClose=updateTaskCheckedItemClose;
      //删除检查项
      scope.delCheckItem=delCheckItem;
      //检查项checkbox
      scope.changeCheckListStatus=changeCheckListStatus;
      // 获取律师 进程列表
      scope.EditAddPeopleBox=EditAddPeopleBox;
      scope.EditAddPeopleClose=EditAddPeopleClose;
      //添加律师执行人
      scope.addTaskLawyer=addTaskLawyer;
      //删除律师执行人
      scope.deleteTaskLaywer=deleteTaskLaywer;

      //附件

      //判断文件类型
      scope.ShowFileSuffix=ShowFileSuffix;
      //删除附件
      scope.DeleteTaskAppendix=DeleteTaskAppendix;
      //删除附件 证据
      scope.deleteProofFile=deleteProofFile;
      //下载附件
      scope.DownloadFileProgress=DownloadFileProgress;
      //案件附件预览
      scope.TaskPreviewAttach=TaskPreviewAttach;
      //案件附件预览关闭
      scope.taskPreviewAttachClose=taskPreviewAttachClose;
      //时间选择器
      // scope.dateTime=dateTime;
      //删除时间
      scope.deleteTaskDeadLine=deleteTaskDeadLine;

      //评论
      scope.sendComment=sendComment;
      //添加评论
      scope.editTaskSendComment=editTaskSendComment;
      //获取证据下拉列表
      scope.getEGroupList=getEGroupList;
      //显示 证据list
      scope.caseProofListToggle=caseProofListToggle;
      scope.caseGroupListToggle=caseGroupListToggle;



      //获取证据下拉列表
       function getEGroupList() {
        caseBarDetailZhengService.getEGroupList(scope.caseId).then(function (data) {
          if (data.code == 1) {
            scope.EGroupList = data.data;
            for(var a=0;a<scope.EGroupList.length;a++){
              if(scope.EGroupList[a].id==scope.tempDetail.task.evidenceGroupId){
                //换算 初始分类
                scope.caseGroupListConversion=scope.EGroupList[a].evidenceGroupName;
              }
            }
          }
        })
      };
       function getETypeList(){
        caseBarDetailZhengService.getETypeList().then(function (data) {
          if (data.code == 1) {
            scope.ETypeList = data.data;
            for(var a=0;a<scope.ETypeList.length;a++){
              if(scope.ETypeList[a].id==scope.tempDetail.task.evidenceTypeId){
                //换算 初始分类
                scope.caseProofListConversion=scope.ETypeList[a].evidenceTypeName;
              }
            }
          }
        })
      };
      function caseProofListToggle(){
        if($rootScope.typeIf!=7){
          scope.getCaseProofList=!scope.getCaseProofList;
        }
      }
      function caseGroupListToggle(){
        if($rootScope.typeIf!=7){
          scope.getCaseGroupList=!scope.getCaseGroupList;
        }

      }
      //编辑任务案件
      function EditTaskCase(){
         scope.editTaskCase=true;
        scope.detailEditToggle=false;
      }
      //编辑任务状态
      function DetailEditToggle (){
        if($rootScope.typeIf!=7){
          scope.detailEditToggle=!scope.detailEditToggle
        }

      }
      function EditTaskStateClose(){
        scope.tempDetail.task.content=scope.detail.task.content;
        scope.editTaskState=false;
      }
      //编辑 任务描述
      function EditCaseDescribe(){
        if($rootScope.typeIf!=7){
          scope.tempDetail.task.content==undefined?scope.editTaskState=true:''
        }
      }
      function EditCaseDescribeTwo(){
        if($rootScope.typeIf!=7){
          scope.tempDetail.task.content!=undefined?scope.editTaskState=true:''
        }
      }
      //删除任务进程
        function jcdeleteupdateNameConfirm($event) {
          if($rootScope.typeIf!=7){
            bootbox.dialog({
              message: "确定删除?",
              title: "确定删除任务进程?",
              className:'d-promptBox',
              buttons: {
                OK: {
                  label: "确定",
                  className: "d-btn-confirm",
                  callback: function () {
                    if(scope.tempDetail.taskType==1){
                      caseBarDetailjinchengService.updateTask(scope.detailTaskId,"9",scope.tempDetail.task.title,scope.loginId,scope.caseId,scope.userName).then(function (data) {
                        if(data.code == 1){
                          scope.parentList.splice(parseInt(scope.current),1)
                          // changeName()
                          EditTaskParticularsClose()
                          //关闭进程confirm弹窗
                          $('#jcdeletedeleteModalpanel').modal('hide');
                        }
                      })
                    }else{
                      caseBarDetailZhengService.deleteEDetail(scope.caseId,scope.detailTaskId,scope.loginId).then(function(data){
                        if(data.code == 1){

                          // changeName()
                          scope.parentList.splice(parseInt(scope.current),1);

                          EditTaskParticularsClose()
                          //关闭进程confirm弹窗
                          $('#jcdeletedeleteModalpanel').modal('hide');
                        }
                      })
                    }

                  }
                },
                Cancel: {
                  label: "取消",
                  className: "d-btn-cancel",
                  style:'background:red',
                  callback: function () {

                  }
                }

              }
            });
          }

          $event.stopPropagation();
      };

      //点击checkbox删除进程
       function detailThrothTheP ($event,id1,cheackStatus) {
      /*  var cheackBox = $event.target;
        var $cheackBox = $(cheackBox);*/
        $event.stopPropagation();
     if($rootScope.typeIf!=7){
       if(cheackStatus==0){

         caseBarDetailjinchengService.updateTask(id1,"1",scope.tempDetail.task.title,scope.loginId,scope.caseId,scope.userName).then(function (data) {
           if(data.code == 1){
             scope.tempDetail.task.status='1';

             //刷新工作台
             if($state.current.name=='workBarNoWork'){
               scope.parentList.splice(parseInt(scope.current),1);
               //关闭详情
               EditTaskParticularsClose();
             }else{
               changeName();
             }


           }
         });
       }else {
         caseBarDetailjinchengService.updateTask(id1,"0",scope.tempDetail.task.title,scope.loginId,scope.caseId,scope.userName).then(function (data) {
           if(data.code == 1){
             scope.tempDetail.task.status='0';

             //刷新工作台
             if($state.current.name=='workBarNoWork'){
               scope.parentList.splice(parseInt(scope.current),1);
               //关闭详情
                EditTaskParticularsClose()
             }else{
               changeName();
             }
           }
         });
       }

     }

      };
      //提交
      function updateDes(id,evidenceGroupIdx,$event){
        var caseProofListId=id!=null?scope.ETypeList[id].id:scope.tempDetail.task.evidenceTypeId;
        var evidenceGroupId=evidenceGroupIdx!=null?scope.EGroupList[evidenceGroupIdx].id:scope.tempDetail.task.evidenceGroupId;
        if(scope.taskType==1){
          var params = {
            "taskId":scope.detailTaskId,
            "content": scope.tempDetail.task.content,
            "deadLine":scope.tempDetail.task.deadline,
            "title":scope.tempDetail.task.title,
            "creatorId":scope.loginId,
            "caseId":scope.caseId,
            "userName":scope.userName,
            "estimateHour":scope.tempDetail.task.estimateHour,
            'actualHour':scope.tempDetail.task.actualHour
          };
          caseBarDetailjinchengService.updateDateOrDes(params).then(function (data) {
            if(data.code==1){
              scope.editTaskCase=false;
              scope.detailEditToggle=false;
              scope.editTaskEstimateHour=false;
              scope.editTaskActualHour=false;
              scope.editTaskState=false;
              getTaskLogList()
              changeName()
            }

          })
        }else{
            var params={
              "evidenceId":scope.detailTaskId,//证据ID
              "creatorId":scope.loginId,
              "status":"0",
              "evidenceTypeId":caseProofListId,
              "description":scope.tempDetail.task.content,//任务描述
              "remark":null,
              "evidenceGroupId":evidenceGroupId,//证据类型
              "evidenceName":scope.tempDetail.task.title,
              "estimateHour":scope.tempDetail.task.estimateHour,//实际工时
              "actualHour":scope.tempDetail.task.actualHour//预计工时
            };
            caseBarDetailZhengService.updateEDetail(params).then(function (data) {//updateDetailXInput
              if (data.code == 1) {
                scope.tempDetail.task.evidenceGroupId=evidenceGroupId;
                scope.tempDetail.task.evidenceTypeId=caseProofListId;
                // 分组换算 初始分类
                if(evidenceGroupIdx!=null){
                  scope.caseGroupListConversion=scope.EGroupList[evidenceGroupIdx].evidenceGroupName;
                }
                // 类型换算 初始分类
                if(id!=null){
                  scope.caseProofListConversion=scope.ETypeList[id].evidenceTypeName;
                }
                scope.editTaskCase=false;
                scope.detailEditToggle=false;
                scope.editTaskEstimateHour=false;
                scope.editTaskActualHour=false;
                scope.editTaskState=false;
                scope.getCaseProofList=false;
                scope.getCaseGroupList=false;
                getELog()
                changeName()
              }
            });

        }
        if($event){
          $event.stopPropagation();
        }

      }
      //工时
      function EditTaskEstimateHour($event){
        if($rootScope.typeIf!=7){
          scope.editTaskEstimateHour=true
        }

      }
      function EditTaskActualHour($event){
        if($rootScope.typeIf!=7){
          scope.editTaskActualHour=true
        }

      }

      //检查项
      function EditCheckItem(){
        if($rootScope.typeIf!=7){
          scope.editCheckItemToggle=true;
        }

      }
      function EditCheckItemClose(){
        scope.editCheckItemToggle=false;
      }
      function EditTaskCheckName (caseTaskCheckList){
        if($rootScope.typeIf!=7){
          if(caseTaskCheckList.status!=5){
            caseTaskCheckList.editTaskCheckName=true;
          }
        }
      }
      //检查项添加
      scope.taskCheck={};
      function saveTaskCheckedItem($event) {
        var event=$event.target;
        if(scope.taskCheck.checkname!='' && scope.taskCheck.checkname != undefined){
          if(scope.taskType==1){
            var params = {"taskId":scope.detailTaskId,"checkName":scope.taskCheck.checkname};
            caseBarDetailjinchengService.saveTaskCheckedItem(params).then(function (response) {
              if(response.data.code == 1){
                var item = {
                  "checkname":scope.taskCheck.checkname,
                  "id":response.data.data.id
                };
                scope.tempDetail.caseTaskCheckList.push(item);
                scope.taskCheck.checkname='';
                scope.editCheckItemHint=false;
                scope.editCheckItemToggle=false;
                getTaskLogList()
                changeName()
              }
            })
          }else{
            var params = {'checkName':scope.taskCheck.checkname,'evidenceId':scope.detailTaskId};
            caseBarDetailZhengService.checkedEvidenceAdd(params).then(function (response){
              if(response.data.code == 1){
                var item = {
                  "checkname":response.data.data.checkname,
                  "id":response.data.data.id,
                };
                scope.tempDetail.caseTaskCheckList.push(item);
                scope.taskCheck.checkname='';
                scope.editCheckItemHint=false;
                scope.editCheckItemToggle=false;
                getELog()
                changeName()
              }
            })
          }
        }else{
          scope.editCheckItemHint=true;
        }
      }
      //检查项 checkbox
      function changeCheckListStatus(caseTaskCheckList,$event) {
        switch (caseTaskCheckList.status){
          case 1: caseTaskCheckList.status = 5;
            break;
          case 5: caseTaskCheckList.status = 1;
            break;
          case undefined: caseTaskCheckList.status = 5;
            break;
        }
        updateTaskCheckedItem(caseTaskCheckList);
        $event.stopPropagation();
      }
      //检查项接口更新
      function updateTaskCheckedItem(caseTaskCheckList,$event) {

        if(scope.tempDetail.taskType==1){
          var params = {"caseTaskCheckId":caseTaskCheckList.id,"checkStatus":caseTaskCheckList.status,"taskId":scope.detailTaskId,"checkName": caseTaskCheckList.checkname};
          caseBarDetailjinchengService.updateTaskCheckedItem(params).then(function (response) {
            if (response.data.code == 1) {
              caseTaskCheckList.editTaskCheckName=false;
              caseTaskCheckList .status=caseTaskCheckList .status;
              getTaskLogList()
              changeName()
            }
          })
        }else{
          var paramss = {"checkId":caseTaskCheckList.id,"checkStatus":caseTaskCheckList.status,"checkName":caseTaskCheckList.checkname};
          caseBarDetailZhengService.checkedEvidenceEdit(paramss).then(function (response){
            if(response.data.code == 1){
              caseTaskCheckList .status=caseTaskCheckList .status;
              caseTaskCheckList.editTaskCheckName=false;
              getELog()
              changeName()
            }
          })
        }
        // $event.stopPropagation();
      }
      function updateTaskCheckedItemClose (caseTaskCheckList, $event){

        caseTaskCheckList.editTaskCheckName=false;
        $event.stopPropagation();
      }
      //删除检查项
      function delCheckItem(index,caseTaskCheckList,$event) {
        if($rootScope.typeIf!=7){
          bootbox.dialog({
            message: "确定删除检查项?",
            title: "删除检查项",
            className:'d-promptBox',
            buttons: {
              OK: {
                label: "确定",
                className: "d-btn-confirm",
                callback: function () {
                  if(scope.tempDetail.taskType==1){
                    var params = {"caseTaskCheckId":caseTaskCheckList.id,"taskId":scope.detailTaskId,"checkStatus":9};
                    caseBarDetailjinchengService.updateTaskCheckedItem(params).then(function (response) {
                      if(response.data.code == 1){
                        scope.tempDetail.caseTaskCheckList.splice(index,1);
                        getTaskLogList()
                        changeName()
                      }
                    });
                  }else{
                    var params = {"checkId":caseTaskCheckList.id};
                    caseBarDetailZhengService.checkedEvidenceDel(params).then(function (response) {
                      if(response.data.code == 1){
                        scope.tempDetail.caseTaskCheckList.splice(index,1);
                        getELog()
                        changeName()
                      }
                    });
                  }

                }
              },
              Cancel: {
                label: "取消",
                className: "d-btn-cancel",
                style:'background:red',
                callback: function () {

                }
              }

            }
          });
        }

        $event.stopPropagation();
      }
      //获取律师进程列表
      function EditAddPeopleBox(IfTypeToogle){
        if($rootScope.typeIf!=7){
          !IfTypeToogle&&(scope.editAddPeopleToggle=true);
          crashBox()
          if(scope.taskType==1){
            caseBarDetailjinchengService.getLawyers(scope.caseId,scope.detailTaskId).then(function (data) {
              if(data.code == 1){
                scope.lawyersList = data.data;
              }
            });
          }else{
            caseBarDetailZhengService.getLawyers(scope.caseId,scope.detailTaskId).then(function(data){
              if(data.code == 1){
                scope.lawyersList = data.data;

              }
            })
          }
        }


      }

      function EditAddPeopleClose($event){
        scope.editAddPeopleToggle=false;
        $event.stopPropagation();
      }
      //添加律师执行人
      function addTaskLawyer (index,lawyerId,lawyerName,$event) {
        var Index=index;
        if(scope.taskType==1){
          caseBarDetailjinchengService.addTaskLawyer(scope.detailTaskId,lawyerId,lawyerName,scope.loginId,scope.userName,scope.caseId).then(function (data) {
            if(data.code == 1){
              var execute=scope.lawyersList.splice(Index,1);
              var taskExecute={
                'executor':execute[0].lawyer,
                'id':data.data,
                'executiveid':execute[0].lawyerid
              };
              scope.tempDetail.taskExecutors.push(taskExecute);
              changeName()
              crashBox()
              getTaskLogList()
            }

          },function (response) {
            console.error('addTaskLawyer error', response.status, response.statusText);
          });
        }else{
          caseBarDetailZhengService.addLawyers(scope.detailTaskId,lawyerId,lawyerName, scope.loginId, scope.detailTaskId,scope.tempDetail.task.evidenceName,scope.caseId).then(function(data){
            if(data.code == 1){
              var execute=scope.lawyersList.splice(Index,1);
              var taskExecute={
                'executor':execute[0].lawyer,
                'id':data.data,
                'executiveid':execute[0].lawyerid
              };
              scope.tempDetail.taskExecutors.push(taskExecute);
              changeName()
              crashBox()
              getELog()
            }
          })
        }

        $event.stopPropagation();
      };
      //删除律师执行人
      function deleteTaskLaywer(lawyerId,executiveid,executiveName,index,$event){
        $event.stopPropagation();
        if($rootScope.typeIf!=7){
          bootbox.dialog({
            message: "确定删除律师?",
            title: "删除律师",
            className:'d-promptBox',
            buttons: {
              OK: {
                label: "确定",
                className: "d-btn-confirm",
                callback: function () {
                  if(scope.tempDetail.taskType==1){
                    caseBarDetailjinchengService.deleteTaskLawyer(lawyerId,scope.loginId,scope.userName,executiveid,executiveName,scope.detailTaskId).then(function (data) {
                      if(data.code == 1){
                        scope.tempDetail.taskExecutors.splice(index,1);
                        //刷新添加列表
                        EditAddPeopleBox(true);
                        getTaskLogList()
                        changeName()
                      }
                    },function (response) {

                    });
                  }else{
                    caseBarDetailZhengService.deleteLawyers(lawyerId,executiveid,executiveName,scope.loginId,scope.detailTaskId).then(function(data){
                      if(data.code == 1){
                        scope.tempDetail.taskExecutors.splice(index,1);
                        //刷新添加列表
                        EditAddPeopleBox(true);
                        getELog()
                        changeName()
                      }
                    })
                  }
                }
              },
              Cancel: {
                label: "取消",
                className: "d-btn-cancel",
                style:'background:red',
                callback: function () {

                }
              }

            }
          });
        }

      }
      //附件上传
      if($rootScope.typeIf!=7){
        element.find('.d-appendix-input').change(function () {
          if(ckPhoto(this)){
            if(scope.tempDetail.taskType==1){
              var fileData = new FormData();
              fileData.append('filename1',this.files[0]);
              fileData.append('taskId',scope.detailTaskId);
              fileData.append('creatorId',scope.loginId);
              caseBarDetailjinchengService.uploadAppendix(fileData).then(function (response) {
                if (response.code == 1) {
                  var item = {
                    'creatUserId':response.data.creatUserId,
                    'id':response.data.id,
                    'taskId':response.data.taskId,
                    'fileSize':response.data.fileSize,
                    'fileName':response.data.fileName,
                    'filePath':response.data.filePath,
                  };
                  scope.tempDetail.caseTaskAttses.push(item);
                  element.find(".appendix-input").val("");
                  getTaskLogList()
                  changeName()
                }
              }, function (response) {
                console.error('uploadAppendix error', response.status, response.statusText);
              });

            }else{
              var Fdata = new FormData();
              Fdata.append('filename1',this.files[0]);
              Fdata.append('evidenceId',scope.detailTaskId);
              Fdata.append('creatorId',scope.loginId);
              Fdata.append('caseid',scope.caseId);
              caseBarDetailZhengService.uploadFile(Fdata).then(function (data) {
                if(data.code==1){
                  //刷新证据的详情的信息
                  var item = {
                    'creatorId':data.data.creatorId,
                    'evidenceId':data.data.evidenceId,
                    'filePact':data.data.filePact,
                    'fileSize':data.data.fileSize,
                    'id':data.data.id,
                    'name':data.data.name,
                  };
                  scope.tempDetail.caseTaskAttses.push(item);
                  element.find(".appendix-input").val("");
                  getELog()
                  changeName()
                }
              })

            }
          }

        });
      }
      function ckPhoto(obj) {
       var photoExt = obj.value.substr(obj.value.lastIndexOf(".")).toLowerCase();
       var photoExtArr=['.mp3','.jpg,mp4','.pdf','.png','.docx','.pptx','.xlsx','.doc','.ppt','.xls'];
        if(photoExtArr.indexOf(photoExt)>-1) {

        }else{
          bootbox.dialog({
            message: "不支持该文件类型，目前支持文件类型为（mp3,jpg,mp4,pdf,png,docx,pptx,xlsx,doc,ppt,xls）",
            title: "提示信息",
            className:'d-promptBox',
            buttons: {
              OK: {
                label: "确定",
                className: "d-btn-confirm5",
                callback: function () {

                }
              }
            }
          });
          return false;
        }
        var fileSize = 0;
        fileSize = obj.files[0].size;

        fileSize = Math.round(fileSize / 1024 * 100) / 100;
        if(fileSize >= 10240*2) {
          bootbox.dialog({
            message: "上传文件大小不能超过20M，请重新选择上传",
            title: "提示信息",
            className:'d-promptBox',
            buttons: {
              OK: {
                label: "确定",
                className: "d-btn-confirm5",
                callback: function () {

                }
              }
            }
          });
          return false;
        }
        return true;
      }

      //判断文件类型
       function ShowFileSuffix (fileName) {
        var suffix = "";
        if(fileName){
          var str= fileName.split(".");
          suffix = str[str.length-1];
          suffix = suffix.toLowerCase();
          var fileTypeList = [ 'xlsx' , 'doc' , 'html' , 'jpg', 'pdf' , 'png' , 'ppt' , 'txt' , 'xls' , 'docx'];
          if(fileTypeList.indexOf(suffix) == -1){
            suffix = "empty";
          }
        }
        return suffix;
      };
      //删除附件
       function DeleteTaskAppendix (fileId,fileName,createrId,index) {

        if($rootScope.typeIf!=7){
          bootbox.dialog({
            message: '确定要删除'+fileName+'附件吗',
            title: "提示信息",
            className:'d-promptBox',
            buttons: {
              OK: {
                label: "确定",
                className: "d-btn-confirm",
                callback: function () {
                  caseBarDetailjinchengService.deleteTaskAppendix(fileId,fileName,scope.detailTaskId,createrId).then(function (data) {
                    if(data.code == 1){
                     scope.tempDetail.caseTaskAttses.splice(index,1);
                      getTaskLogList()
                      changeName()
                    }
                  },function (response) {
                    console.error('deleteTaskAppendix error', response.status, response.statusText);
                  });
                }
              },
              Cancel: {
                label: "取消",
                className: "d-btn-cancel",
                callback: function () {

                }
              }

            }
          });
        }
      };
      //删除附件 证据
      function deleteProofFile (id,name,index) {
        if($rootScope.typeIf!=7){
          bootbox.dialog({
            message: '确定要删除'+name+'附件吗',
            title: "提示信息",
            className:'d-promptBox',
            buttons: {
              OK: {
                label: "确定",
                className: "d-btn-confirm",
                callback: function () {
                    /*caseBarDetailZhengService.deleteFile(id,scope.detailTaskId,name,scope.loginId,scope.caseId).then(function (data) {
                      if(data.code == 1){
                        getELog()
                        //刷新证据的详情的信息
                        scope.tempDetail.caseTaskAttses.splice(index,1);
                        changeName()
                      }
                    });*/

                  caseBarDetailZhengService.deleteEvidenceAttr(id,scope.detailTaskId).then(function (data) {

                    if(data.data.code == 1){
                      getELog()
                      //刷新证据的详情的信息
                      scope.tempDetail.caseTaskAttses.splice(index,1);
                      changeName()
                    }
                  });

                }
              },
              Cancel: {
                label: "取消",
                className: "d-btn-cancel",
                callback: function () {

                }
              }

            }
          });
        }
      };
      //下载附件
      function DownloadFileProgress (fileX) {
        if(scope.tempDetail.taskType==1){
          var params = {'caseId':scope.caseId,'taskId':scope.detailTaskId,'taskAttachedId':fileX.id};
          caseBarDetailjinchengService.downloadAppendix(params).then(function (response) {
            var fileData = new Blob([response.data]);
            var link = document.createElement('a');
            link.href = URL.createObjectURL(fileData);
            link.download = fileX.fileName;
            link.click();
          });
        }else{
          var paramss = {'caseid':scope.caseId,'evidenceId':scope.detailTaskId,'evidenceAttachedId':fileX.id};
          caseBarDetailZhengService.downloadAttachment(paramss).then(function (response) {
            var file = new Blob([response.data]);
            var link = document.createElement('a');
            link.href = URL.createObjectURL(file);
            link.download = fileX.name;
            link.click();
            // window.URL.revokeObjectURL(link.href);
          });
        }

      };
      //案件进程--附件预览
      scope.taskAttach = false;
       function TaskPreviewAttach (file) {

        $('#myModalTaskPreview').modal('show');
        var fileName = file.fileName || file.name;
        var suffix = "";
        if(fileName){
          var str= fileName.split(".");
          suffix = str[str.length-1];
          suffix = suffix.toLowerCase();
          var fileTypeList = [ 'jpg', 'pdf' , 'png','JPG', 'PDF' , 'PNG'  ];
          if(fileTypeList.indexOf(suffix) != -1){
            // 清空上次信息
            // pdfDelegate.$getByHandle('my-pdf-container').load();
            // $('.y-preview-img2').attr('src','');
            if (suffix != 'pdf') {
              $('#pdfViewerCanvas').hide();
              $('.y-preview-img2').show();
            } else {
              $('#pdfViewerCanvas').show();
              $('.y-preview-img2').hide();
            }
            $('.y-preview').attr('data-toggle','modal');
            $('.y-preview').attr('data-target','#myModalTaskPreview');

            if(scope.tempDetail.taskType==1){

              var params = {'caseId':scope.caseId,'taskId':scope.detailTaskId,'taskAttachedId':file.id};
              caseBarDetailjinchengService.taskPreviewAttach(params).then(function (response) {
                var fileData = new Blob([response.data]);
                var link = URL.createObjectURL(fileData);
                scope.taskAttach = true;
                // link.toString();
                if (suffix != 'pdf')
                  $rootScope.preViewHref = link;
                else
                  pdfDelegate.$getByHandle('my-pdf-container').load(link);
                // window.URL.revokeObjectURL(link);
              })
            } else if(scope.tempDetail.taskType == 2){
              var params = {'caseId':scope.caseId,'evidenceId':scope.detailTaskId,'evidenceAttachedId':file.id};
              caseBarDetailZhengService.evidencePreviewAttach(params).then(function (response) {
                var fileData = new Blob([response.data]);
                var link = URL.createObjectURL(fileData);
                scope.taskAttach = true;
                // link.toString();
                if (suffix != 'pdf')
                  $rootScope.preViewHref = link;
                else
                  pdfDelegate.$getByHandle('my-pdf-container').load(link);
                // window.URL.revokeObjectURL(link);
              })
            }
          }else {
            $('.y-preview').attr('data-toggle',' ');
            $('.y-preview').attr('data-target',' ');
            bootbox.dialog({
              message: "此文件类型不支持预览",
              title: "提示信息",
              className:'d-promptBox',
              buttons: {
                OK: {
                  label: "确定",
                  className: "d-btn-confirm",
                  callback: function () {
                  }
                },
                Cancel: {
                  label: "取消",
                  className: "d-btn-cancel",
                  callback: function () {

                  }
                }
              }
            });
          }
        }
      };
      //案件进程--关闭预览
       function taskPreviewAttachClose () {
        scope.taskAttach = false;
        window.URL.revokeObjectURL($rootScope.preViewHref);
      };
      //时间选择器 更改截至日期
      // function dateTime(){
        if($rootScope.typeIf!=7){
          element.find('.deadlineDatePicker').one("mouseover", function () {
            var deadlineTaskId='.editTaskDeadline_'+scope.detailTaskId;
            $(deadlineTaskId).datetimepicker({
              minView: "month",
              format: 'yyyy-mm-dd',
              language: "zh-CN",
              charset: "utf-8"
            }).on('changeDate', function (ev) {
              $(deadlineTaskId).datetimepicker('hide');
              var d = new Date(ev.date);
              var month = d.getMonth() + 1 > 9 ? d.getMonth() + 1 : '0' + (d.getMonth() + 1);
              var day = d.getDate() > 9 ? d.getDate() : '0' + d.getDate();
              var date = d.getFullYear() + '-' + month + '-' + day;
              scope.tempDetail.task.deadline = date;
              if(scope.tempDetail.taskType == 1) {
                //案件进程
                var params = {
                  "taskId":scope.detailTaskId,
                  "content": scope.tempDetail.task.content,
                  "deadline":scope.tempDetail.task.deadline,
                  "title":scope.tempDetail.task.title,
                  "creatorId":scope.loginId,
                  "caseId":scope.caseId,
                  "userName":scope.userName,
                  "estimateHour":scope.tempDetail.task.estimateHour,
                  'actualHour':scope.tempDetail.task.actualHour
                };
                caseBarDetailjinchengService.updateDateOrDes(params).then(function (data) {
                  if(data.code == 1){
                    //更新日志
                    getTaskLogList()
                    //刷新详情
                    changeName()
                  }
                });
              } else if(scope.tempDetail.taskType == 2){
                //证据任务
                caseBarDetailZhengService.updateEDDate(scope.detailTaskId,scope.loginId,scope.tempDetail.task.deadline).then(function (data) {
                  if (data.code == 1) {
                    //更新证据日志
                    getELog()
                    //刷新证据的详情的信息
                    changeName()
                  }
                });
              }
            });
          });

        }
      // }

      //删除日期
     function deleteTaskDeadLine(item,$event) {
        if($rootScope.typeIf!=7){
          var params={
            "taskId":item.id,
            "caseId":item.caseid||item.caseId
          };
          bootbox.dialog({
            message: "确定要删除截至日期",
            title: "提示信息",
            className:'d-promptBox',
            buttons: {
              OK: {
                label: "确定",
                className: "d-btn-confirm",
                callback: function () {
                  if(scope.taskType==1){
                    workBarService.deleteTaskDeadLine(params).then(function (data) {
                      if(data.code == 1){
                        scope.tempDetail.task.deadline = null;
                        getTaskLogList()
                        changeName()

                      }
                    })
                  }else{
                    caseBarDetailZhengService.updateEDDate(scope.detailTaskId,scope.loginId,null).then(function(data){
                      if(data.code == 1){
                        scope.tempDetail.task.deadline = null;
                        getELog ()
                        changeName()
                      }
                    })
                  }

                }
              },
              Cancel: {
                label: "取消",
                // className: "btn-default",
                // className: "btn-cancel",
                // confirmButtonColor: "#DD6B55",
                className: "d-btn-cancel",
                callback: function () {
                }
              }
            }
          });
        }
        $event.stopPropagation();
      };


      //评论
      function sendComment(toogle){

        toogle==1?scope.sendCommentTab=false:scope.sendCommentTab=true;

      }
       function editTaskSendComment(text) {
         if(text!=''){
           if(scope.tempDetail.taskType==1){
             caseBarDetailjinchengService.sendJCComment(scope.loginId,text,scope.detailTaskId).then(function (data) {
               if(data.code == 1){
                 //刷新详情
                 scope.tempDetail.caseTaskReviews.unshift(data.data)
                 getTaskLogList ()
                 scope.commentInput = '';
                 changeName()
               }
             })
           }else{
             caseBarDetailZhengService.insertEComment(scope.detailTaskId,scope.loginId, scope.commentInput).then(function (data) {
               if (data.code == 1) {
                 scope.tempDetail.caseTaskReviews.unshift(data.data);
                 getELog();
                 scope.commentInput = '';
                 changeName()

               }
             });
           }

         }

      };

      //关闭任务进程
      function EditTaskParticularsClose(){
        if(parseInt(element.css('right'))==0){
          scope.tempDetail = angular.copy(scope.detail);
          element.animate({right:'-700px'});
          $timeout(function(){
            element.css({display:'none'});
          },301)

        }
      }
     // 碰撞检测
     function crashBox(){
       var crash=element.find('#d-addLastLawyer')[0].offsetLeft+element.find('#d-addLastLawyer')[0].offsetWidth+220;
       var father=element[0].offsetWidth;

       if(crash>=father){
         element.find('#d-addLastLawyerSon').css({'left':'-220px'})
       }else{
         element.find('#d-addLastLawyerSon').css({'left':'40px'})
       }
     }
      element.on('click',function(ev){
        var ev=ev||event;
        ev.stopPropagation();
      })
      element.parent().on('click',function(ev){

       var ev=ev||event;
        scope.detailEditToggle=false;
        scope.editTaskState=false;
        scope.editTaskEstimateHour=false;
        scope.editTaskActualHour=false;
        scope.editCheckItemToggle=false;
        scope.editAddPeopleToggle=false;
        scope.editCheckItemHint=false;
        scope.getCaseProofList=false;
        scope.getCaseGroupList=false;
        scope.init()
        scope.$apply();

        if(scope.taskType==2){
          getEGroupList()
          getETypeList()
        }
        if(parseInt(element.css('right'))!=0){
          $('.d-closeSiblings').stop().animate({right:'-700px',display:'none'});
          element.css({display:'block'});
          element.stop().animate({right:0});
        }
       ev.stopPropagation();

     })
      //任务详情数据同步
      function changeName() {
        scope.detail = angular.copy(scope.tempDetail);
      }
      //任务日志数据同步
      function getTaskLogList () {
        caseBarDetailjinchengService.getTaskLog(scope.detailTaskId).then(function (response) {
          if(response.code == 1){
            scope.tempDetail.taskLogs= response.data;

          }
        }/*,function (response) {
          console.error('getTaskLogList error', response.status, response.statusText);
        }*/)
      }
      function getELog () {
        caseBarDetailZhengService.getELog(scope.detailTaskId).then(function (data) {
          if (data.code == 1) {
            scope.tempDetail.taskLogs= data.data;
          }
        });
      };

    }

  }

};
